import { Context, Next } from 'koa';
import Joi from 'joi';

export const validate = (schema: Joi.ObjectSchema) => async (ctx: Context, next: Next) => {
    const { error } = schema.validate(ctx.request.body);
    if (error) {
        ctx.status = 400;
        ctx.setResponse(null, error.details[0].message, 400);
        return;
    }
    await next();
};